package com.example.dudu_music_bridge;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

import androidx.annotation.Nullable;

import com.dudu.music.bridge.DuduMusicCallback;
import com.dudu.music.bridge.DuduMusicInterface;
import com.dudu.music.bridge.Music;

import java.util.ArrayList;
import java.util.List;

public class MusicService extends Service {
    private DuduMusicCallback duduMusicCallback;
    private final DuduMusicInterface duduMusicInterface = new DuduMusicInterface.Stub() {

        @Override
        public List<Music> getMusicList() throws RemoteException {
            List<Music> music = new ArrayList<>();
            music.add(new Music().setIndex(0).setTitle("TEST").setCover("Cover").setPath("PATH").setSonger("SONGER").setTotal(10000));
            return music;
        }

        @Override
        public void musicCmd(int cmd) throws RemoteException {
            
        }

        @Override
        public void musicSearch(String key) throws RemoteException {

        }

        @Override
        public void setCallback(DuduMusicCallback cb) throws RemoteException {
            duduMusicCallback = cb;
            if (cb != null) {
                final IBinder binder = cb.asBinder();
                binder.linkToDeath(new DeathRecipient() {
                    public void binderDied() {
                        duduMusicCallback = null;
                        binder.unlinkToDeath(this, 0);
                    }
                }, 0);
            }
        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return duduMusicInterface.asBinder();
    }
}
